<!--
 * @Descripttion: 
 * @Version: 1.0
 * @Author: wcl
 * @Date: 2023-11-21 16:54:39
 * @LastEditors: wcl
 * @LastEditTime: 2023-11-23 17:02:31
-->
<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="品牌管理">
        <a-form :model="formState">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="品牌名称">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.searchCondition"
                  placeholder="输入类目名称"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="品牌标签">
                <a-select
                  ref="select"
                  v-model:value="formState.tag"
                  style="width: 100%"
                  placeholder="状态"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">热门品牌</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="状态">
                <a-select
                  ref="select"
                  v-model:value="formState.state"
                  style="width: 100%"
                  placeholder="状态"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">启用</a-select-option>
                  <a-select-option :value="2">禁用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handelOpen('新增品牌', {})"
              >新增品牌</a-button
            >
            <a-button type="primary" @click="handleExport">导出</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="brandColumns"
          :pagination="pagination"
          @change="handleChangeSize"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'coverPic'">
              <a-image
                :src="record.coverPic"
                width="100px"
                height="100px"
              ></a-image>
            </template>
            <template v-if="column.key === 'typeId'">
              {{ record.typeId }}
            </template>
            <template v-if="column.key === 'backgroundImage'">
              <a-image :src="record.backgroundImage" width="100px"></a-image>
            </template>
            <template v-if="column.key === 'isHot'">
              <a-tag>{{ record.isHot == 1 ? "热门品牌" : "全部" }}</a-tag>
            </template>
            <template v-if="column.key === 'state'">
              {{ record.state == 1 ? "启用" : "禁用" }}
            </template>
            <template v-if="column.key === 'operate'">
              <a-space>
                <a-button type="primary" @click="handelOpen('编辑品牌', record)"
                  >编辑</a-button
                >
                <a-popconfirm
                  :title="`您是否要${
                    record.state == 1 ? '启用' : '禁用'
                  }该品牌?`"
                  @confirm="handleStateChange(record)"
                >
                  <a-button v-if="record.state == 1" danger type="primary"
                    >禁用</a-button
                  >
                  <a-button v-else type="primary">启用</a-button>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </a-table>
      </a-card>
    </a-space>
    <addBrand ref="brandRef" @refreshList="refreshList" />
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { brandColumns } from "./columns.js";
import addBrand from "./categoryModule/addBrand.vue";
import { brandList, brandState, brandExport } from "@/api/goodConfig.js";
import { exporeFile } from "@/utils/exporeFile";
import { message } from "ant-design-vue";
const pagination = ref({
  current: 1,
  pageSize: 10,
  defaultPageSize: 10,
  total: 0,
  showSizeChanger: true,
  pageSizeOptions: ["10", "20", "50", "100"],
  showTotal: () => `共 ${pagination.value.total} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  searchCondition: undefined,
  state: undefined,
  tag: undefined,
});
const brandRef = ref(null);
const dataSource = ref([]);
function handleSearch() {
  formState.value.page = 1;
  formState.value.pageSize = 10;
  getBrandList();
}
const handelOpen = (title, record) => {
  brandRef.value.open(title, record);
};
// 品牌列表
const getBrandList = async () => {
  const { data } = await brandList(formState.value);
  dataSource.value = data.list;
  pagination.value.total = data.total;
};
function handleReset() {
  formState.value = {
    page: 1,
    pageSize: 10,
    searchCondition: undefined,
    state: undefined,
    tag: undefined,
  };
  pagination.value.current = 1
  pagination.value.pageSize = 10
  getBrandList();
}

const handleExport = async () => {
  const data = await brandExport(formState.value);
  exporeFile("品牌列表.xls", data);
};
function handleStateChange(record) {
  brandState({ id: record.id, state: record.state == 1 ? 2 : 1 }).then(
    (res) => {
      if (res.code === 200) {
        message.success("修改成功");
        getBrandList();
      }
    }
  );
}
function handleChangeSize(page) {
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  pagination.value.current = page.current;
  pagination.value.pageSize = page.pageSize;
  getBrandList();
}
function refreshList() {
  getBrandList();
}
onMounted(() => {
  getBrandList();
});
</script>

<style lang="less">
.img-item {
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}
</style>
